﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Excel;
namespace BYManageInfoSystem
{
    public partial class Frm_LogisticsTruePayRegMonth : Form
    {
        private DGBY.BLL.LogisticsTruePayRegTab m_bllNotLogisticsTruePayRegTab = new DGBY.BLL.LogisticsTruePayRegTab();
        public Frm_LogisticsTruePayRegMonth()
        {
            InitializeComponent();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllNotLogisticsTruePayRegTab.GetAllLogisticsTruePayMonthRegList("PayMonth = '" + this.textBoxMonth.Text + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                Decimal dt = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dt += Convert.ToDecimal(ds.Tables[0].Rows[i]["实付金额"].ToString());
                }

                DataRow dr = ds.Tables[0].NewRow();
                dr["编号"] = "合计";
                dr["实付金额"] = dt;
                ds.Tables[0].Rows.Add(dr);
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void ultraGridExcelExporter1_CellExported(object sender, Infragistics.Win.UltraWinGrid.ExcelExport.CellExportedEventArgs e)
        {
            Worksheet ws = e.CurrentWorksheet;
            if (e.CurrentColumnIndex == 0)
            {
                Infragistics.Excel.IWorksheetCellFormat cfCellFmt;
                Infragistics.Excel.IWorksheetCellFormat cfCellFmt2;
                string stringt = "";
                if (e.Value != null)
                {
                    stringt = e.Value.ToString();
                    //e.Value = null;
                }
                if (stringt == "合计")
                {
                    int iRdex = e.CurrentRowIndex;
                    int iCdex = e.CurrentColumnIndex;
                    //ws.Rows[iRdex].Cells[iCdex - 1].Value = "";

                    cfCellFmt = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex].CellFormat;
                    cfCellFmt.LeftBorderStyle =
          Infragistics.Excel.CellBorderLineStyle.None;
                    cfCellFmt2 = e.CurrentWorksheet.Rows[iRdex].Cells[iCdex + 1].CellFormat;
                    cfCellFmt2.RightBorderStyle =
          Infragistics.Excel.CellBorderLineStyle.None;

                    // Create a merged region that will be a header to the column headers
                    Infragistics.Excel.WorksheetMergedCellsRegion mergedRegion1 =
                      ws.MergedCellsRegions.Add(iRdex, iCdex, iRdex, iCdex + 1);

                    // Set the value of the merged region
                    mergedRegion1.Value = stringt;

                    // Give the merged region a solid background color
                    mergedRegion1.CellFormat.FillPattern =
                      Infragistics.Excel.FillPatternStyle.Solid;
                    mergedRegion1.CellFormat.FillPatternForegroundColor = Color.White;

                    // Set the cell alignment of the middle cell in the merged region.
                    // Since a cell and its merged region shared a cell format, this will 
                    // ultimately set the format of the merged region
                    ws.Rows[iRdex].Cells[iCdex].CellFormat.Alignment =
                      Infragistics.Excel.HorizontalCellAlignment.Center;
                }

            }
        }
    }
}
